<%@ page contentType="text/html;charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="tags" tagdir="/WEB-INF/tags"%>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags"%>
<c:set var="ctx" value="${pageContext.request.contextPath}" />
<html>
<head>
<title>菜单管理</title>
<script src="${ctx}/static/selectWin/appSelector.js?version=${applicationScope.jsVersion }" type="text/javascript"></script>
<script type="text/javascript">
	var selectedMenu;
	var appSelectOpts;
	var menuGrid;
	$(function() {
		menuGrid = $("#menuGrid").treegrid({
			toolbar : "#toolbar",
			//checkOnSelect : false,// 选择行将不选中复选框
			method : "get",
			url : "${ctx}/wx/wxMenu/treeGrid",
			idField : 'id',
			treeField : 'menuName',
			remoteSort : false,

			dndRow : true, //此属性开启此表格的行拖动排序功能

			singleSelect : true,
			striped : true,
			selectOnCheck : false,
			checkOnSelect : false,

			dataPlain : true, //该属性用以启用当前 easyui-treegrid 控件对平滑数据格式的支持
			parentField : "menuParentId",
			columns : [ [ {
				field : 'id',
				checkbox : true
			}, {
				field : 'menuName',
				title : '菜单名称',
				sortable : true
			}, {
				field : 'menuType',
				title : '菜单动作',
				sortable : true
			}, {
				field : 'menuKey',
				title : '菜单Key值',
				sortable : true
			}, {
				field : 'mediaId',
				title : '媒体ID',
				sortable : true
			}, {
				field : 'app',
				title : '所属公众号',
				sortable : true,
				formatter : function(value, row, index) {
					return value.appName;
				}
			}, {
				field : 'menu',
				title : '所属菜单',
				hidden : 'true'
			}, {
				field : 'menuUrl',
				title : '菜单URL',
				sortable : true,
				formatter : function(val) {
					if (val != null && val.length > 25) {
						return val.substr(0, 25) + "...";
					} else {
						return val;
					}
				}
			}, {
				field : 'oauthState',
				title : '是否需要授权',
				formatter : function(val) {
					return val == 1 ? "是" : "否";
				},
				sortable : true,
			}, {
				field : 'stateName',
				title : '处理路径',
				sortable : true,
			} ] ],
		});

		$("#appSelector").click(function() {
			$.easyui.showGridSelector($.extend(appSelectOpts, {
				multiple : false,
				singleSelect : true,
				onEnter : function(val) {
					var appId = val.id;
					var appName = val.appName;
					$("#appId").val(appId);
					$("#appName").val(appName);
				}
			}));
		});

	});

	//新增子节点
	function fun_menu_add_child() {
		var row = menuGrid.treegrid("getSelected");
		if (row.menu != null) {
			showAlert("只能创建两级菜单!");
		}
		if (row == null) {
			showAlert("您未选择任何记录!");
		} else {
			$("#dlg").dialog("open").dialog("setTitle", "新增下级菜单");
			$("#fm").form("clear").form("load", {
				oauthState : 2
			});
			if (row.app) {
				$("#appId").val(row.app.id);
				$("#appName").val(row.app.appName);
				//$("#appSelector").hide();
			}
			$("#parentMenuId").val(row.id);
			$("#parentMenuName").val(row.menuName);
		}
	}

	//新增同级节点
	function fun_menu_add() {
		var row = menuGrid.treegrid("getSelected");
		if (row == null) {
			showAlert("您未选择任何记录!");
		} else {
			$("#dlg").dialog("open").dialog("setTitle", "新增同级节点");
			$("#fm").form("clear").form("load", {
				oauthState : 2
			});

			if (row.menu) {
				$("#parentMenuId").val(row.menu.id);
				$("#parentMenuName").val(row.menu.menuName);

				if (row.app) {
					$("#appId").val(row.app.id);
					$("#appName").val(row.app.appName);
				}

				$("#appSelector").hide();
			}
		}
	}

	//右键新增子节点
	/* function fun_menu_add_child1(index) {
		if (index == null) {
			showAlert("您未选择任何记录!");
		} else {
			$("#dlg").dialog("open").dialog("setTitle", "新增下级菜单");
			$("#fm").form("clear").form("load", {
				menuParentId : index.id,
				rootId : index.rootId == "-1" ? index.id : index.rootId
			});
		}
	}

	//右键新增同级节点
	function fun_menu_add1(index) {
		if (index == null) {
			showAlert("您未选择任何记录!");
		} else {
			$("#dlg").dialog("open").dialog("setTitle", "新增同级节点");
			$("#fm").form("clear").form("load", {
				menuParentId : index.menuParentId,
				rootId : index.rootId == "-1" ? "-1" : index.rootId
			});
		}
	} */

	//新增根节点
	function fun_menu_add_root() {
		$("#dlg").dialog("open").dialog("setTitle", "新增根节点菜单");
		$("#fm").form("clear").form("load", {
			oauthState : 2
		});
		$("#parentMenuId").val("");
	}

	function fun_menu_edit() {
		var row = menuGrid.treegrid("getSelected");
		if (row == null) {
			showAlert("您未选择任何记录!");
		} else {
			$("#dlg").dialog("open").dialog("setTitle", "编辑菜单--[" + row.menuName + "]");
			$("#fm").form("clear");
			$("#fm").form("load", row);
			if (row.app) {
				$("#appId").val(row.app.id);
				$("#appName").val(row.app.appName);
			}
			if (row.menu) {
				$("#parentMenuId").val(row.menu.id + "");
				$("#parentMenuName").val(row.menu.menuName);
			}
		}
	}

	function fun_menu_save() {
		$("#fm").form("submit", {
			url : "${ctx}/wx/wxMenu/update",
			success : function(result) {
				if (result.success) {
					$("#dlg").dialog("close");
					menuGrid.treegrid("load");
				}
				showInfo(result.msg);
			}
		});
		reviewForm();
	}

	//删除
	function fun_menu_del() {
		multiDeleteTreeGrid(menuGrid, "${ctx}/wx/wxMenu/multiDelete");

	}

	function fun_menu_syn() {
		var rowData = menuGrid.datagrid("getSelected");
		if (!rowData) {
			showInfo("请先选中相应菜单！");
			return false;
		}
		var url = "${ctx}/wx/wxMenu/updateMenu";
		$.post(url, {
			originAppId : rowData.app ? rowData.app.originAppId : "",
			appId : rowData.app ? rowData.app.id : ""
		}, function(data) {
			if (data.success) {
				$.messager.show({
					msg : data.msg,
					icon : "info",
					position : "bottomRight"
				});
				//showInfo(data.msg)
			}
		}, "json");
	}

	function cancel() {
		$('#dlg').dialog('close');
		reviewForm();
	}

	//解除禁用的弹出框
	function reviewForm() {
		$("#appSelector").show();
	}
</script>
</head>
<body>
	<div data-options="region:'center'" style="padding: 1px;">
		<div id="cc" class="easyui-layout" data-options="fit:true" style="width: 480px; height: 440px;">
			<div data-options="region:'center'" style="padding: 1px; witdh: 600px">
				<table id="menuGrid" data-options="fit:true">
				</table>
				<div id="toolbar">
					<shiro:hasPermission name="menu:update">
						<a id="btn-add" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain: true" onclick="fun_menu_add_root()">新增根节点</a>
						<a id="btn-add" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain: true" onclick="fun_menu_add()">新增同级节点</a>
						<a id="btn-add" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain: true" onclick="fun_menu_add_child()">新增子节点</a>
						<a id="btn-edit" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-edit',plain: true" onclick="fun_menu_edit()">修改</a>
					</shiro:hasPermission>
					<shiro:hasPermission name="menu:delete">
						<a id="btn-delete" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain: true" onclick="fun_menu_del()">删除</a>
					</shiro:hasPermission>
					<a id="updateMenu" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-hamburg-exchange',plain: true"
						onclick="fun_menu_syn()">同步菜单</a>
					<%-- <a id="btn-delete" href="#" class="easyui-linkbutton" data-options="" onclick="TreeGridDelete('#menuGrid','${ctx}/basic/menu/delete')">删除</a> --%>
				</div>
				<shiro:hasPermission name="menu:update">
					<div id="dlg" class="easyui-dialog" style="width: 400px; height: 380px; padding: 10px 20px"
						data-options="closed:true,buttons:'#dlg-buttons'">
						<form id="fm" method="post">
							<input id="id" type="hidden" name="id" /> <input id="menuParentId" type="hidden" name="menuParentId" />
							<table id="tbl">
								<tr>
									<td><label>所属公众号:</label></td>
									<td><input id="appId" name="app" type="hidden"><input id="appName" readonly="readonly" /><a id="appSelector"
										class="easyui-linkbutton easyui-tooltip" data-options="plain: true, iconCls: 'icon-metro-search',content:'选取公共账号'"></a></td>
								</tr>
								<tr>
									<td><label>父菜单:</label></td>
									<td><input id="parentMenuId" name="menu" type="hidden"><input id="parentMenuName" name="parentMenuName"
										readonly="readonly"></td>
								</tr>
								<tr>
									<td><label>菜单名称:</label></td>
									<td><input name="menuName" class="easyui-validatebox" data-options="required:true"></td>
								</tr>
								<tr>
									<td><label>菜单动作类型:</label></td>
									<td><input name="menuType" class="easyui-validatebox" data-options="required:true"></td>
								</tr>
								<tr>
									<td><label>菜单Key值:</label></td>
									<td><input name="menuKey" class="easyui-validatebox"></td>
								</tr>
								<tr>
									<td><label>媒体ID:</label></td>
									<td><input name="mediaId" class="easyui-validatebox"></td>
								</tr>
								<tr>
									<td><label>需要授权:</label></td>
									<td><input id="radio1" name="oauthState" type="radio" value="1" /> <label for="radio1">是</label> <input id="radio2"
										name="oauthState" type="radio" value="2" /> <label for="radio2">否</label></td>
								</tr>
								<tr>
									<td><label>处理路径:</label></td>
									<td><input name="stateName" class="easyui-validatebox"></td>
								</tr>
								<tr>
									<td><label>菜单url:</label></td>
									<td><textarea name="menuUrl" rows="8" cols="25" readonly="readonly"></textarea></td>
								</tr>
							</table>
						</form>
					</div>
					<div id="dlg-buttons">
						<a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-ok'" onclick="fun_menu_save()">保存</a> <a href="#"
							class="easyui-linkbutton" data-options="iconCls:'icon-cancel'" onclick="cancel()">取消</a>
					</div>
				</shiro:hasPermission>
			</div>
		</div>
	</div>
</body>
</html>
